﻿#include "AreaTimeZoneDescEx.h"
#include "DescStore/AreaTimeZoneDesc.h"

AreaTimeZoneDescEx::AreaTimeZoneDescEx()
{
	if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetCreateMode()) {
		CreateInit();
	}
	else {
		ResumeInit();
	}
}

AreaTimeZoneDescEx::~AreaTimeZoneDescEx()
{
}

int AreaTimeZoneDescEx::CreateInit()
{
    m_iIndex = -1;
	return 0;
}

int AreaTimeZoneDescEx::ResumeInit()
{
	return 0;
}

int AreaTimeZoneDescEx::Load()
{
	return 0;
}

int AreaTimeZoneDescEx::GetTimezoneOffset()
{
    if (m_iIndex < 0 || m_iIndex >= AreaTimeZoneDesc::Instance()->GetResNum())
    {
        return 0;
    }

    const E_AreaTimeZone* pDesc = AreaTimeZoneDesc::Instance()->GetDescByIndex(m_iIndex);
    if (pDesc)
    {
        return pDesc->m_timeZoneOffset;
    }

    return 0;
}

int AreaTimeZoneDescEx::CheckWhenAllDataLoaded()
{
#ifdef I18N_GLOBAL
    int appId = NFGlobalSystem::Instance()->GetGlobalPluginManager()->GetAppID();
    int regionId = NFServerIDUtil::GetRegionID(appId);
    int worldId = NFServerIDUtil::GetWorldID(appId);
    int pubZoneId = NFServerIDUtil::GetPubZoneID(worldId);
    m_iIndex = -1;
    if (AreaTimeZoneDesc::Instance()->GetResNum() <= 0)
    {
        LOG_ERR(0, -1, "AreaTimeZoneDescEx::CheckWhenAllDataLoaded no data");
        return -1;
    }

    const int iMaxTimezoneOffset = 12 * 3600;

    for (m_iIndex = 0; m_iIndex < AreaTimeZoneDesc::Instance()->GetResNum(); ++m_iIndex)
    {
        if (GetTimezoneOffset() > iMaxTimezoneOffset || GetTimezoneOffset() < -iMaxTimezoneOffset)
        {
            LOG_ERR(0, -1, "CDescStoreAreaTimezone TimezoneOffset(%d) error", GetTimezoneOffset());
            return -1;
        }

        auto pDesc = AreaTimeZoneDesc::Instance()->GetDescByIndex(m_iIndex);
        CHECK_NULL_WF(0, pDesc, "AreaTimeZoneDesc::Instance()->GetDescByIndex failed, index:{}", m_iIndex);
        if (pDesc->m_regionID * 10 > pDesc->m_areaID)
        {
            LOG_ERR(0, -1, "CDescStoreAreaTimezone RegionID({}) * 10 > AreaID({}) error", pDesc->m_regionID, pDesc->m_areaID);
            return -1;
        }

        if (pDesc->m_regionID != regionId || pDesc->m_areaID - pDesc->m_regionID * 10 != pubZoneId)
        {
            continue;
        }

        NFTimeUtility::SetTimeZoneOffset(pDesc->m_timeZoneOffset);
        return 0;
    }
#endif
	return 0;
}

